package com.zangchat.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
    /**
     * 查询过去月份（包含本月）
     * @param months
     * @return
     */
    public static List<Integer> getLastMonths(int months){
        List<Integer> list =new ArrayList<>();
        for (int i = months-1; i >=0; i--) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.MONTH, -i);
            Date m = c.getTime();
            list.add(Integer.parseInt(sdf.format(m)));
        }
        return list;
    }

    /**
     * 过去N天（不包含今天）
     * @param days
     * @return
     */
    public static List<Integer> getLastDays(int days){
        List<Integer> list =new ArrayList<>();
        for (int i = days; i >=1; i--) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, -i);
            Date m = c.getTime();
            list.add(Integer.parseInt(sdf.format(m)));
        }
        return list;
    }

    public static String strToDateFormat(String date) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        formatter.setLenient(false);
        Date newDate= formatter.parse(date);
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(newDate);
    }

    /**
     * 今日转换成String
     * @return
     */
    public static String todayToStr(){
        Date date= new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }
    /**
     * 日期String转换成Date
     * @return
     */
    public static Date strToDate(String date) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        formatter.setLenient(false);
        Date newDate= formatter.parse(date);
        return newDate;
    }
    /**
     * 日期Date转换成String
     * @return
     */

    public static String dateToStr(Date date) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }
    /**
     * 月份String转换成Date
     * @return
     */
    public static Date strToMonth(String date) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        formatter.setLenient(false);
        Date newDate= formatter.parse(date);
        return newDate;
    }
    /**
     * 月份Date转换成String
     * @return
     */
    public static String monthToStr(Date date) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        return formatter.format(date);
    }


    /**
     * 查询上个月
     * @param
     * @return
     */
    public static Date getLastMonths(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        return m;

    }

    /**
     * 两个时间之间差多少天
     * @param startTime
     * @param endTime
     * @return
     */
    public static Long dateDiff(Date startTime, Date endTime) {
        //按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        //获得两个时间的毫秒时间差异
        long diff;
        diff = endTime.getTime() - startTime.getTime();
        Long days = diff / nd ;//计算差多少天
        return  days;

    }
}
